package sk.lukaskrupa.guess.the.color.listeners;

import sk.lukaskrupa.guess.the.color.core.ApplicationConstants;
import sk.lukaskrupa.guess.the.color.core.GameVariantSpecificActions;
import sk.lukaskrupa.guess.the.color.logging.Logging;
import sk.lukaskrupa.guess.the.color.ui.Game;
import sk.lukaskrupa.guess.the.color.ui.R;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

/**
 * Provide handlers for common button actions.
 * 
 * @author Lukas Krupa (lukas0krupa@gmail.com)
 * 
 */
public class ColorPickerButtonListener implements OnClickListener {

	private static Logging logger = Logging.getLog(ColorPickerButtonListener.class);

	private final Game main;
	private ImageButton invokedOn;
	private final GameVariantSpecificActions gameVariants;

	/**
	 * Creating Listener and saving mainView, for callback.
	 * 
	 * @param game
	 * @param gameVariants
	 */
	public ColorPickerButtonListener(Game game, GameVariantSpecificActions gameVariants) {
		this.main = game;
		this.gameVariants = gameVariants;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onClick(View clickedButton) {
		logger.debug("onClick", "Color picker clicked.");
		logger.debug("onClick", "You clicked button ID=" + clickedButton.getId() + ".");

		int oldColor = (this.invokedOn.getTag(ApplicationConstants.DRAWABLE_ID) != null) ? (Integer) this.invokedOn
				.getTag(ApplicationConstants.DRAWABLE_ID) : 0;
		// Getting color
		switch (clickedButton.getId()) {
			case R.id.popup_button1 : {
				logger.debug("onClick", "Changing invoked button color to: BLACK");
				changeInvokerButtonColor(R.drawable.black, this.invokedOn.getId());
				// Disabling picked color till next round
				this.gameVariants.hideAccordingToSettings(clickedButton, oldColor, R.drawable.black_disabled);
				break;
			}
			case R.id.popup_button6 : {
				logger.debug("onClick", "Changing invoked button color to: BLUE");
				changeInvokerButtonColor(R.drawable.blue, this.invokedOn.getId());
				// Disabling picked color till next round
				this.gameVariants.hideAccordingToSettings(clickedButton, oldColor, R.drawable.blue_disabled);
				break;
			}
			case R.id.popup_button3 : {
				logger.debug("onClick", "Changing invoked button color to: GREEN");
				changeInvokerButtonColor(R.drawable.green, this.invokedOn.getId());
				// Disabling picked color till next round
				this.gameVariants.hideAccordingToSettings(clickedButton, oldColor, R.drawable.green_disabled);
				break;
			}
			case R.id.popup_button2 : {
				logger.debug("onClick", "Changing invoked button color to: RED");
				changeInvokerButtonColor(R.drawable.red, this.invokedOn.getId());
				// Disabling picked color till next round
				this.gameVariants.hideAccordingToSettings(clickedButton, oldColor, R.drawable.red_disabled);
				break;
			}
			case R.id.popup_button4 : {
				logger.debug("onClick", "Changing invoked button color to: WHITE");
				changeInvokerButtonColor(R.drawable.white, this.invokedOn.getId());
				// Disabling picked color till next round
				this.gameVariants.hideAccordingToSettings(clickedButton, oldColor, R.drawable.white_disabled);
				break;
			}
			case R.id.popup_button5 : {
				logger.debug("onClick", "Changing invoked button color to: YELLOW");
				changeInvokerButtonColor(R.drawable.yellow, this.invokedOn.getId());
				// Disabling picked color till next round
				this.gameVariants.hideAccordingToSettings(clickedButton, oldColor, R.drawable.yellow_disabled);
				break;
			}
			case R.id.popup_button7 : {
				logger.debug("onClick", "Changing invoked button color to: BROWN");
				changeInvokerButtonColor(R.drawable.brown, this.invokedOn.getId());
				// Disabling picked color till next round
				this.gameVariants.hideAccordingToSettings(clickedButton, oldColor, R.drawable.brown_disabled);
				break;
			}
			case R.id.popup_button8 : {
				logger.debug("onClick", "Changing invoked button color to: PURPLE");
				changeInvokerButtonColor(R.drawable.purple, this.invokedOn.getId());
				// Disabling picked color till next round
				this.gameVariants.hideAccordingToSettings(clickedButton, oldColor, R.drawable.purple_disabled);
				break;
			}
		}

	}
	/**
	 * Changes color on button which invoked color picker.
	 * 
	 * @param newColor
	 *            to use
	 * @param buttonId
	 *            button that was clicked
	 */
	private void changeInvokerButtonColor(int newColor, int buttonId) {

		logger.debug("changeInvokerButtonColor", "Changin color on buttonId=" + buttonId + " to color with ID=" + newColor);

		this.invokedOn.setImageDrawable(this.main.getResources().getDrawable(newColor));
		this.invokedOn.setTag(ApplicationConstants.DRAWABLE_ID, newColor);

		this.main.closeColorPicker();

		switch (buttonId) {
			case R.id.button1 : {
				this.main.getBoard().setButtonColortTo(1, newColor);
				break;
			}
			case R.id.button2 : {
				this.main.getBoard().setButtonColortTo(2, newColor);
				break;
			}
			case R.id.button3 : {
				this.main.getBoard().setButtonColortTo(3, newColor);
				break;
			}
			case R.id.button4 : {
				this.main.getBoard().setButtonColortTo(4, newColor);
				break;
			}
			case R.id.button5 : {
				this.main.getBoard().setButtonColortTo(5, newColor);
				break;
			}
		}
	}

	/**
	 * Saves reference to invoked button.
	 * 
	 * @param invokedButton
	 *            id
	 */
	public void setInvokedOn(ImageButton invokedButton) {
		this.invokedOn = invokedButton;
	}

}
